package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysEvaluates;
import com.ruoyi.project.system.domain.SysOrderEvaluate;
import com.ruoyi.project.system.service.ISysEvaluatesService;
import com.ruoyi.project.system.service.ISysOrderEvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobileUser/orderComment/")
public class UserOrderCommentController {

    @Autowired
    private ISysOrderEvaluateService sysOrderEvaluateService;
    @Autowired
    private ISysEvaluatesService sysEvaluatesService;
    @Autowired
    private SysEvaluates sysEvaluates;

    //  用户登录之后发表物品订单评论
    @PostMapping("addComment")
    public AjaxResult addCommnet(@RequestBody SysOrderEvaluate sysOrderEvaluate) {
        List<String> list = new ArrayList<String>();
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysOrderEvaluate.setMobileUserId(mobileUserId);
        sysOrderEvaluateService.insertSysOrderEvaluate(sysOrderEvaluate);
        list = sysOrderEvaluate.getUrls();
        if (null!=list) {
            for (int i = 0; i < list.size(); i++) {
                sysEvaluates.setUrl(list.get(i));
                sysEvaluates.setEvaluateId(sysOrderEvaluate.getEvaluateId());
                sysEvaluatesService.insertSysEvaluates(sysEvaluates);
            }
        }
        return AjaxResult.success();
    }

    //  用户查看订单 评论 以及 图片
    @GetMapping("queryAllOrder")
    public AjaxResult queryAllOrder(SysOrderEvaluate sysOrderEvaluate){
        Map map = new HashMap();
        List<SysOrderEvaluate> sysOrderEvaluates = sysOrderEvaluateService.selectSysOrderEvaluateList(sysOrderEvaluate);
        List<SysEvaluates> sysEvaluates = sysEvaluatesService.selectSysEvaluatesById(sysOrderEvaluate.getEvaluateId());
        map.put("sysOrderComment",sysOrderEvaluates);
        map.put("sysEvaluates",sysEvaluates);
        return AjaxResult.success(map);
    }

    // 回复二手评论   传上级评论的id  evaluateId  ——>

    @PostMapping("replyOrderComment")
    public AjaxResult replyOrderComment(@RequestBody SysOrderEvaluate sysOrderEvaluate){
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysOrderEvaluate.setParentValuateId(sysOrderEvaluate.getEvaluateId());
        sysOrderEvaluate.setMobileUserId(mobileUserId);
        return AjaxResult.success(sysOrderEvaluateService.insertSysOrderEvaluate(sysOrderEvaluate));
    }

    // 用户删除自己的评论
    @PostMapping("deleteComment")
    public AjaxResult deleteComment(@RequestBody SysOrderEvaluate sysOrderEvaluate){
        sysOrderEvaluateService.deleteSysOrderEvaluateById(sysOrderEvaluate.getEvaluateId());
        sysEvaluatesService.deleteSysEvaluatesById(sysOrderEvaluate.getEvaluateId());
        sysOrderEvaluateService.deleteSysOrderEvaluateById(sysOrderEvaluate.getEvaluateId());
        return AjaxResult.success();
    }
}
